//@author A0097978X
package flexiCommands;

import java.lang.StringBuilder;

public class ParseDisplayInfo extends ParseInfo{

	private static final String ERROR_MESSAGE_TOO_FEW_ARGUMENT = "Too little info";

	private static final int QUERY_TYPE = 1;
	private static final int COMMAND = 0;
	private static final int UNPROCESSED = 0;
	private static final int TWO_ARGUMENTS = 2;
	
	private static final String ONE_SPACE = " ";
	
	private String[] infoToParse;
	private Command processedCmd;
	private int[] typeDeterminer;

	public ParseDisplayInfo(String[] userInfo) throws Exception{
		super(userInfo);

		if(userInfo.length < TWO_ARGUMENTS)
			throw new Exception(ERROR_MESSAGE_TOO_FEW_ARGUMENT);
	}

	@Override
	public Command execute() {
		infoToParse = super.infoString;
		processedCmd = super.cmdObj;

		processedCmd.setCommandType(infoToParse[COMMAND]);

		typeDeterminer = new int[infoToParse.length];

//====================== Flexible Arguments Type =======================//

		for(int i=1; i<infoToParse.length; i++) {
			if(typeDeterminer[i] == UNPROCESSED)	{
				typeDeterminer[i] = QUERY_TYPE;
			}
		}

//=================== Parse & Generate Respective Type ===================//
		if(checkTypeDeterminer()) {
			generateQuery();
		}

		return processedCmd;
	}

	private boolean checkTypeDeterminer() {
		for(int i=1; i<typeDeterminer.length; i++) {
			if(typeDeterminer[i] == UNPROCESSED)
				return false;
		}

		return true;
	}

	private void generateQuery() {
		StringBuilder query = new StringBuilder();
		for(int i=1; i<typeDeterminer.length; i++) {
			if(typeDeterminer[i] == QUERY_TYPE) {
				query.append(infoToParse[i] + ONE_SPACE);
			}
		}

		processedCmd.setQuery(query.toString().trim());
	}

}